package com.ktwlsoft.archivemanage.controller.study;

import com.ktwlsoft.archivemanage.config.FileStorageProperties;
import com.ktwlsoft.archivemanage.domain.business.businessService.FileService;
import com.ktwlsoft.archivemanage.domain.business.businessService.study.StudyClassService;
import com.ktwlsoft.archivemanage.domain.dao.entity.study.StudyClass;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.study.studyClass.SetInvalidRequest;
import com.ktwlsoft.archivemanage.payload.study.studyClass.StudyClassPageRequest;
import com.ktwlsoft.archivemanage.payload.study.studyClass.StudyClassPayload;
import com.ktwlsoft.archivemanage.payload.study.studyClass.UpDownRequest;
import io.swagger.annotations.ApiOperation;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/study/class")
public class StudyClassController {

    private final StudyClassService studyClassService;

    private final FileStorageProperties fileStorageProperties;

    public StudyClassController(StudyClassService studyClassService,
                                FileStorageProperties fileStorageProperties) {
        this.studyClassService = studyClassService;
        this.fileStorageProperties = fileStorageProperties;
    }

    @PostMapping("/manage/save")
    public ResponseEntity<?> save(@RequestBody final StudyClassPayload request) {
        studyClassService.saveClass(request, StudyClass.ClassType.MANAGE);
        return ResponseEntity.ok(Result.ok("操作完成!"));
    }

    @PostMapping("/user/save")
    public ResponseEntity<?> userSave(@RequestBody final StudyClassPayload request) {
        studyClassService.saveClass(request, StudyClass.ClassType.USER);
        return ResponseEntity.ok(Result.ok("操作完成!"));
    }

    @DeleteMapping("/delete")
    public ResponseEntity<?> delete(@RequestParam final Long id) {
        studyClassService.deleteClass(id);
        return ResponseEntity.ok(Result.ok("操作完成!"));
    }

    @GetMapping("/manage/list")
    public ResponseEntity<?> manageList(final StudyClassPageRequest request) {
        return ResponseEntity.ok(Result.ok(studyClassService.findList(request, StudyClass.ClassType.MANAGE)));
    }

    @GetMapping("/user/list")
    public ResponseEntity<?> userList(final StudyClassPageRequest request) {
        return ResponseEntity.ok(Result.ok(studyClassService.findList(request, StudyClass.ClassType.USER)));
    }

    @PutMapping("/updown")
    public ResponseEntity<?> classUpDown(@RequestBody UpDownRequest upDownRequest) {
        return ResponseEntity.ok(Result.ok(studyClassService.changeState(upDownRequest.getId(), upDownRequest.getState(), upDownRequest.getBannerShow(),upDownRequest.getClassType())));
    }

    @GetMapping("/isValid")
    public ResponseEntity<?> isValid(@RequestParam final Long id) {
        return ResponseEntity.ok(Result.ok(studyClassService.isValid(id)));
    }

    @ApiOperation("管理端分片上传")
    @PostMapping("/saveManageFile")
    public ResponseEntity<?> saveManageClass(@RequestParam("file") final MultipartFile file, final String fileName,
                                             final int sliceNum, final Boolean isLast) {
        try {
            return ResponseEntity.ok(Result.ok(studyClassService.saveFile(file, fileName, sliceNum, isLast, fileStorageProperties.getManageClassDir())));
        } catch (final Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }

    @ApiOperation("用户端分片上传")
    @PostMapping("/saveUserFile")
    public ResponseEntity<?> saveUserClass(@RequestParam("file") final MultipartFile file, final String fileName,
                                           final int sliceNum, final Boolean isLast) {
        try {
            return ResponseEntity.ok(Result.ok(studyClassService.saveFile(file, fileName, sliceNum, isLast, fileStorageProperties.getManageClassDir())));
        } catch (final Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }

    @ApiOperation("管理端上传文件")
    @PostMapping("/manage/uploadImg")
    public ResponseEntity<?> manageUploadImg(@RequestParam MultipartFile file) throws IOException {
        return ResponseEntity.ok(Result.ok(studyClassService.uploadImg(file, "MANAGE")));
    }

    @ApiOperation("用户端端上传文件")
    @PostMapping("/user/uploadImg")
    public ResponseEntity<?> userUploadImg(@RequestParam MultipartFile file) throws IOException {
        return ResponseEntity.ok(Result.ok(studyClassService.uploadImg(file, "USER")));
    }

    @PostMapping("/setInvalidState")
    @ApiOperation("违规处理")
    public ResponseEntity<?> setInvalidState(@Valid @RequestBody final SetInvalidRequest request) {
        return ResponseEntity.ok(Result.ok(studyClassService.setManageState(request)));
    }
}
